Developing a Game Engine
Related Topics
Game Engines are optional but provide significant advantages for managing game state and enhancing communication efficiency. By running directly on Gig Games servers, game engines can preserve the game state over extended periods, independent of an active host PC. This architecture not only ensures continuity but also enhances the gaming experience by reducing latency, as data does not need to travel back and forth between the server and a host PC.
Advantages of Using Game Engines
- State Preservation: Game engines can maintain the game state even when the host PC is not active, ensuring that game progress and data are never lost.
- Reduced Latency: Hosting the game engine directly on Gig Games servers minimizes communication delays, leading to a smoother and more responsive gaming experience.
- Scalability: Game engines can handle multiple game sessions and players concurrently, leveraging server resources for optimal performance.
- Reliability: With robust server infrastructure, game engines offer high availability and reliability, ensuring that games can continue uninterrupted.
- Automatic Serialization and Deserialization: The Gig Games server automatically serializes and deserializes the state of your game server when it becomes idle, ensuring that the game state is preserved and can be quickly restored when activity resumes.
Implementation Requirements
Create Your JS File: Develop your engine code and place it in a file named
engine.js
.Set Up Your Project Structure: Organize your project directory as shown below. Include the
console
andcontroller
folders for testing and debugging:Example Structure:
/project-root ├── /console │ └── index.html ├── /controller │ └── index.html └── /engine └── engine.js
The engine.js
file should define one class named Engine
that will be instantiated on the server. It does not use the $GG library for communications, but rather implements a specific constructor and interface.
This interface is required for handling events received from the Gig Games server and for broadcasting messages to the console and controllers. These methods are crucial for ensuring seamless communication and integration within the Gig Games ecosystem.
Required Methods for Receiving Messages
Class Constructor:
constructor(id:string, apiKey:string, state: string)
- Called during the intial creation of your game by a host, or when it is restored from and idle state.
- Parameters:
id
: Assigned unique identifier for the game session.apiKey
: API key for authentication.state
: Initial serialized state of the game. If not null, deserialize to restore the state of your game.
trigger:
trigger(string name, ExpandoObject? options, string sourceType, ExpandoObject? source = null) : void
- Required method. This is how you will receive events from controllers and the host.
- Parameters:
name
: Name of the event to trigger.options
: Additional options or data related to the event.sourceType
: Type of the source triggering the event. 'Host' or 'Player'source
: Additional source information for players, and null for hosts. Object:{ playerKey:'', playerName:'', imagePath:'' }
tick:
tick(DateTime engineDate) : void
- Optional method. If you do not implement, tick events will not be utilized for your game.
- Parameters:
engineDate
: The current date and time for the game engine to process time-based events. This is periodically called at 1 sec intervals by a clock from the engine.
serialize:
serialize() : string
- Optional method. If you do not implement, you game will not be serialized, and will be restarted new if allowed to go idle (all players and host abandons).
- Returns: Serialized state of the game as a string.
Exposed Methods for Broadcasting Messages
These methods are provided by the server and are available for your script to use. You do not need to create them yourself, but you can rely on their presence.
broadcastToHost:
broadcastToHost(id: string, eventName: string, options: object)
- Parameters:
id
: Unique identifier for the game session.eventName
: Name of the event to broadcast.options
: Additional event options or data related to the event.
- Parameters:
broadcastToPlayer:
broadcastToPlayer(id: string, playerId: string, eventName: string, options: object)
- Parameters:
id
: Unique identifier for the game session.playerId
: Unique identifier for the player.eventName
: Name of the event to broadcast.options
: Additional options or data related to the event.
- Parameters:
broadcastToPlayers:
broadcastToPlayers(id: string, eventName: string, options: object)
- Parameters:
id
: Unique identifier for the game session.eventName
: Name of the event to broadcast.options
: Additional options or data related to the event.
- Parameters:
setToLeaderboard:
setToLeaderboard(id: string, leaderboardName: string, playerId: string, score: float)
- Parameters:
id
: Unique identifier for the game session.leaderboardName
: Name of the leaderboard.playerId
: Unique identifier for the player.score
: Score to set on the leaderboard.
- Parameters:
syncPlayerSessions:
syncPlayerSessions(id: string)
- Parameters:
id
: Unique identifier for the game session.
- Parameters:
consoleLog:
consoleLog(message: string)
- Parameters:
message
: Message to log to the console. Primarily used for debugging during development.
- Parameters:
Engine Proxy Tool
Gig Games provides a proxy application that simulates the server environment on your workstation. To develop and test your engine code locally, download the appropriate version of the Gig Games proxy tool for your operating system and architecture:
- Windows (32-bit): Download Win-x86 Version
- Windows (64-bit): Download Win-x64 Version
- Windows (ARM64): Download Win-Arm64 Version
- macOS (64-bit): Download OSX-x64 Version
- macOS (ARM64): Download OSX-Arm64 Version
- Linux (64-bit): Download Linux-x64 Version
- Linux (ARM64): Download Linux-Arm64 Version
After downloading, unzip the file into a directory of your choice. This tool will enable you to run and test your engine code locally.
Configuration Settings
To configure the proxy application, you need several keys, which you can obtain from the Gig Games Hub in the application edit screen. Here’s what you need and how to get them:
GameKey: This key identifies your game. It is used to authenticate and link your local engine with the correct game session.
- How to Obtain: Go to the Gig Games Hub, navigate to the application edit screen, and look for the GameKey under your game's configuration settings.
HostKey: This key is used for authentication between the local proxy and the Gig Games server, ensuring secure communication.
- How to Obtain: Similar to the GameKey, you can find the HostKey in the application edit screen of the Gig Games Hub.
ApiKey: This API key provides access to the Gig Games API and is essential for interaction with the server.
- How to Obtain: Retrieve the ApiKey from the Gig Games Hub in the same section where you find the GameKey and HostKey.
Within the unzipped folder, you’ll find a file named appsettings.json
. Update this file with the keys you obtained and other necessary settings:
- GameKey: Your unique game identifier.
- HostKey: For secure communication between the proxy and the server.
- ApiKey: For API access.
- ScriptPath: The path to your
engine.js
file, containing your game engine code. - ServerUrl: The URL of the sandbox server for local development. Set this to
https://sandbox.gig.game
to connect to the sandbox environment for testing. - IsDebug: Set to
true
to enable debugging features during development.
Example Configuration:
{
"GameKey": "your-game-key",
"HostKey": "your-host-key",
"ApiKey": "your-api-key",
"ScriptPath": "path/to/your/engine.js",
"ServerUrl": "https://sandbox.gig.game/masterControlProgram",
"IsDebug": true
}
Run the Proxy Application
After updating the appsettings.json
file with the appropriate values, you can start the proxy application:
Windows
Open a terminal or command prompt.
Navigate to the directory where you unzipped the proxy application.
Execute the application by running
ggEngineProxy.exe
.At this point, you can test by loading your
console/index.html
andcontroller/index.html
in the browser. If everything is set up correctly, you should see communication events streaming in your console.
macOS
Open the Terminal application.
Navigate to the directory where you unzipped the proxy application using the
cd
command.Make the application executable by running:
chmod +x ggEngineProxy
Execute the application by running:
./ggEngineProxy
You can then test by loading your
console/index.html
andcontroller/index.html
in the browser. If everything is set up correctly, you should see communication events streaming in your console.
Linux
- Open a terminal window.
- Navigate to the directory where you unzipped the proxy application using the
cd
command. - Make the application executable by running:
chmod +x ggEngineProxy
- Execute the application by running:
./ggEngineProxy
You can then test by loading your console/index.html
and controller/index.html
in the browser. If everything is set up correctly, you should see communication events streaming in your console.